package club.vann.kafka.mock.util;

import cn.hutool.core.util.RandomUtil;
import com.cyberx.lunyu.Framework;
import com.cyberx.lunyu.QuoteOuterClass;
import java.util.Map;

public class QuoteActionUtil {
  private QuoteActionUtil() {}

  /**
   * 构造 QuoteAction 实例。
   *
   * @param symbol Symbol
   * @param ticker Ticker
   * @param id Id
   * @param action Action
   * @param clientId ClientId
   * @param timestamp Timestamp
   * @param dataMap DataMap
   * @param requestId RequestId
   * @return QuoteAction实例
   */
  public static QuoteOuterClass.Quote.QuoteAction ofQuoteAction(
      String symbol,
      String ticker,
      String id,
      QuoteOuterClass.Quote.QuoteAction.Action action,
      String clientId,
      long timestamp,
      Map<String, String> dataMap,
      String requestId) {
    QuoteOuterClass.Quote.QuoteAction.Builder builder =
        QuoteOuterClass.Quote.QuoteAction.newBuilder()
            .setSymbol(symbol)
            .setTicker(ticker)
            .setAction(action)
            .setClientId(clientId)
            .setTimestamp(timestamp)
            .putAllData(dataMap)
            .setRequestId(requestId);
    if (id != null) {
      builder.setId(id);
    }
    return builder.build();
  }

  /**
   * 构造 QuoteActionResult 实例。
   *
   * @param dataMap 参数集合
   * @return QuoteActionResult 实例
   */
  public static QuoteOuterClass.Quote.QuoteActionResult ofQuoteActionResult(
      Map<String, String> dataMap) {
    String clientId = dataMap.get("CLIENT_ID");
    String venue = dataMap.get("VENUE");
    String ticker = dataMap.get("TICKER");
    String randomChar = RandomUtil.randomString(8);
    String symbol =
        ticker + "-TWAP:" + venue + ":" + clientId + "-QuoteExecutionAlpha-" + randomChar;
    Framework.ErrorCode errorCode = FrameWorkUtil.ofErrorCode(Framework.DataStatus.Green);
    Framework.TimestampRecord timestampRecord = FrameWorkUtil.ofTimestampRecord();
    QuoteOuterClass.Quote.QuoteAction.Action action =
        QuoteOuterClass.Quote.QuoteAction.Action.CONFIRM;
    QuoteOuterClass.Quote.QuoteActionResult.State state =
        QuoteOuterClass.Quote.QuoteActionResult.State.COMPLETED;
    String requestId =
        "ACCEPTOR-CEX-MARKET-DATA-INSTANCE-969404801955496424-1722392512696-" + randomChar;
    return ofQuoteActionResult(
        symbol, errorCode, timestampRecord, action, state, requestId, dataMap);
  }

  /**
   * 构造 QuoteActionResult 实例。
   *
   * @param symbol Symbol
   * @param errorCode ErrorCode
   * @param timestampRecord TimestampRecord
   * @param action Action
   * @param state State
   * @param requestId RequestId
   * @param dataMap DataMap
   * @return QuoteActionResult 实例
   */
  public static QuoteOuterClass.Quote.QuoteActionResult ofQuoteActionResult(
      String symbol,
      Framework.ErrorCode errorCode,
      Framework.TimestampRecord timestampRecord,
      QuoteOuterClass.Quote.QuoteAction.Action action,
      QuoteOuterClass.Quote.QuoteActionResult.State state,
      String requestId,
      Map<String, String> dataMap) {
    return QuoteOuterClass.Quote.QuoteActionResult.newBuilder()
        .setSymbol(symbol)
        .setErrorCode(errorCode)
        .setTimestamp(timestampRecord)
        .setAction(action)
        .setState(state)
        .setRequestId(requestId)
        .putAllData(dataMap)
        .build();
  }
}
